package com.m.android.data.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.m.android.data.db.EnvironmentRecordColums;
import com.m.android.data.db.util.SqliteWrapper;
import com.m.android.data.entity.EnvironmentRecord;

public class EnvironmentRecordDao {
	public static void addEnvironmentRecord(Context context, String path, long time) {
		ContentValues values = new ContentValues();
		values.put(EnvironmentRecordColums.COLUMN_PATH, path);
		values.put(EnvironmentRecordColums.COLUMN_TIME, time);
		SqliteWrapper.insert(context, context.getContentResolver(), EnvironmentRecordColums.CONTENT_URI, values);
	}

	public static List<EnvironmentRecord> getEnvironmentRecords(Context context) {
		Cursor cursor = SqliteWrapper.query(context, context.getContentResolver(), EnvironmentRecordColums.CONTENT_URI, null, null, null, null);
		List<EnvironmentRecord> records = new ArrayList<EnvironmentRecord>();
		if (cursor != null) {
			while (cursor.moveToNext()) {
				EnvironmentRecord record = readFromCursor(cursor);
				records.add(record);
			}
			cursor.close();
		}
		return records;
	}

	private static EnvironmentRecord readFromCursor(Cursor cursor) {
		EnvironmentRecord record = new EnvironmentRecord();
		record.setTime(cursor.getLong(cursor.getColumnIndex(EnvironmentRecordColums.COLUMN_TIME)));
		record.setPath(cursor.getString(cursor.getColumnIndex(EnvironmentRecordColums.COLUMN_PATH)));
		record.setId(cursor.getLong(cursor.getColumnIndex(EnvironmentRecordColums._ID)));
		return record;
	}

	public static int deleteEnvironmentRecord(Context context, long id) {
		return SqliteWrapper.delete(context, context.getContentResolver(), EnvironmentRecordColums.CONTENT_URI, EnvironmentRecordColums._ID + "=?",
				new String[] { String.valueOf(id) });
	}
}
